package com.example.music.adapter;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;
import com.example.music.model.Song;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> implements Filterable {
    private static final String TAG = "SongAdapter";

    private List<Song> originalList;
    private List<Song> filteredList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Song song);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public SongAdapter(List<Song> songList) {
        this.originalList = new ArrayList<>(songList);
        this.filteredList = new ArrayList<>(songList);
    }

    public void updateList(List<Song> newList) {
        originalList.clear();
        originalList.addAll(newList);
        filteredList.clear();
        filteredList.addAll(newList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_song, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Song song = filteredList.get(position);
        if (song == null) {
            Log.w(TAG, "Song at position " + position + " is null");
            return;
        }

        holder.textSongName.setText(song.getTitle());
        holder.textArtist.setText(song.getArtist());
        holder.textDuration.setText(formatDuration(song.getDuration()));

        // 设置专辑封面
        Bitmap bitmap = song.getAlbumCover();
        if (bitmap != null) {
            holder.imageAlbumCover.setImageBitmap(bitmap);
        } else {
            holder.imageAlbumCover.setImageResource(R.drawable.default_album_art); // 设置默认图片
        }

        // 设置点击事件监听器
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(song);
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "Item count: " + filteredList.size());
        return filteredList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                List<Song> filtered = new ArrayList<>();
                for (Song song : originalList) {
                    if (song.getTitle().toLowerCase().contains(constraint.toString().toLowerCase()) ||
                            song.getArtist().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        filtered.add(song);
                    }
                }
                FilterResults results = new FilterResults();
                results.values = filtered;
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredList.clear();
                filteredList.addAll((List<Song>) results.values);
                notifyDataSetChanged();
            }
        };
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textSongName;
        TextView textArtist;
        TextView textDuration;
        ImageView imageAlbumCover;

        ViewHolder(View itemView) {
            super(itemView);
            textSongName = itemView.findViewById(R.id.text_song_name);
            textArtist = itemView.findViewById(R.id.text_artist);
            textDuration = itemView.findViewById(R.id.text_duration);
            imageAlbumCover = itemView.findViewById(R.id.image_album_cover);
        }
    }

    private String formatDuration(long duration) {
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(minutes);
        return String.format("%02d:%02d", minutes, seconds);
    }
}



